package com.pactera.jep.service.sys.web.controller;

import com.pactera.jep.autoconfigure.web.controller.BaseRestController;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.model.RoleGroup;
import com.pactera.jep.sys.service.RoleGroupService;
import com.pactera.jep.sys.service.StaffRoleGroupMapService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.entity.NameValuePair;
import com.pactera.jep.web.util.NVUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色组Controller
 * @author ZhangYan
 * @time 2017年5月31日 上午10:16:47
 * @company 文思海辉技术有限公司
 */
@Api(value="角色组")
@RestController
@RequestMapping("/roleGroup")
public class RoleGroupRestController extends BaseRestController<RoleGroup> {

	Logger logger = LoggerFactory.getLogger(RoleGroupRestController.class);
	
	@Autowired
	RoleGroupService		roleGroupService;
	@Autowired
	StaffRoleGroupMapService staffRoleGroupMapService;
	/**
	 * 为列表页面做数据准备
	 */
	@Override
	protected void prepareQuery(String token,Page<RoleGroup> page, List<Filter> filters, List<Order> orders,HttpServletRequest request) {
		roleGroupService.query(page, filters, orders);
	}
	/**
	 * 保存
	 * @param roleGroup
	 * @return
	 */
	@ApiOperation(value = "保存一条数据")
	@PostMapping(value = "/save")
	public Map<String, Object> save(@RequestBody RoleGroup roleGroup) {
		
		Map<String, Object> tips = new HashMap<String, Object>();
		try
		{
			RoleGroup exist = roleGroupService.get(roleGroup.getRoleGroupCode());
			if(exist == null)
			{
				roleGroupService.insert(roleGroup);
			}
			else
			{
				roleGroupService.update(roleGroup);
			}

			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		} 
		catch (Exception e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
	
		return tips;
	}
	
	/**
	 * 删除
	 * @param code
	 * @return
	 */
	@ApiOperation(value = "根据编码删除一条数据")
	@PostMapping(value = "/remove/{code}")
	public Map<String, Object> remove(@PathVariable String code){
		
		Map<String, Object> tips = new HashMap<String, Object>();
		try 
		{
			this.roleGroupService.removeRoleGroup(code);
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
		} 
		catch (Exception e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
		}
		
		return tips;
	}
	
	/**
	 * 验证提交的roleGroupCode是否已经存在数据库中
	 * @param roleGroupCode
	 * @return
	 */
	@ApiOperation(value = "验证提交的编码是否已经存在数据库中")
	@GetMapping(value = "/check")
	public Map<String, Object> check(String roleGroupCode, HttpServletRequest request){
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		RoleGroup exist = roleGroupService.get(roleGroupCode);
		if(exist == null)
		{
			tips.put("valid", true);
		}
		else
		{
			tips.put("valid", false);
		}
		return tips;
	}
	
	/**
	 * 以特殊格式获取所有角色组类型
	 * @return json格式返回
	 */
	@ApiOperation(value = "以特殊格式获取所有角色组类型")
	@RequestMapping(value = "/queryAll")
	public List<NameValuePair> queryAll(String hierarchy){
		
		List<Filter> filters = new ArrayList<Filter>();
		if(StringUtils.isNotBlank(hierarchy)){
			filters.add(new Filter("hierarchy", hierarchy));
		}
		List<RoleGroup> result = roleGroupService.select(filters);
		
		return NVUtils.convert(result, result.size(), "roleGroupCode", "roleGroupName");
	}

	/**
	 * 根据角色组编码查询角色信息
	 * @param
	 * @param
	 * @return
	 */
	@ApiOperation(value = "根据用户编码查询角色组信息")
	@GetMapping(value = "/selectRoleGroupByStaffCode/{staffCode}")
	public  List<RoleGroup> selectRoleGroupByStaffCode(@PathVariable String staffCode){
		List<RoleGroup> list = new ArrayList<RoleGroup>();
		if(StringUtils.isNotBlank(staffCode)){
			list = staffRoleGroupMapService.getStaffRoleGroupList(staffCode);
		}
		return list;
	}

}
